package com.dkm.biz.gl.repository.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.dkm.base.api.annotion.SKGCacheable;
import com.dkm.base.api.constants.RedisConstants;
import com.dkm.base.api.model.Page;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringCollections;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.gl.constants.GlCateTypeKeys;
import com.dkm.biz.gl.domain.GlCate;
import com.dkm.biz.gl.persistence.dao.GlCateQueryDao;
import com.dkm.biz.gl.persistence.entity.GlCatePo;
import com.dkm.biz.gl.repository.GlCateRepository;
import com.dkm.biz.repository.AbstractRepository;

/**
 * 对象功能:总分类表。用于显示树层次结构的分类，可以允许任何层次结构。 Repository接口的实现类 开发公司:唯心六艺软件 - Craft6.cn
 * 开发人员:winston 创建时间:2014-07-22 11:07:05
 */
@Repository
public class GlCateRepositoryImpl extends AbstractRepository<String, GlCatePo, GlCate> implements GlCateRepository {

	@Resource
	private GlCateQueryDao glCateQueryDao;

	public GlCate newInstance() {
		GlCatePo po = new GlCatePo();
		GlCate glCate = SpringHelper.getBean(GlCate.class);
		glCate.setData(po);
		return glCate;
	}

	public GlCate newInstance(GlCatePo po) {
		GlCate glCate = SpringHelper.getBean(GlCate.class);
		glCate.setData(po);
		return glCate;
	}

	@Override
	protected IQueryDao<String, GlCatePo> getQueryDao() {
		return glCateQueryDao;
	}

	public List<GlCatePo> findTree(String typeKey) {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKey(typeKey);
		return convertToTree(glCatePos);
	}

	@SKGCacheable(db = RedisConstants.SYSTEM, key = "findAllTreeCache_${typeKey}", params = "typeKey")
	public List<GlCatePo> findTreeCache(String typeKey) {
		return findTree(typeKey);
	}

	@SKGCacheable(db = RedisConstants.SYSTEM, key = "findAllTree")
	public List<GlCatePo> findAllTree() {
		List<GlCatePo> glCatePos = glCateQueryDao.findAll();
		return convertToTree(glCatePos);
	}

	@SKGCacheable(db = RedisConstants.SYSTEM, key = "findAllTreeCache")
	public List<GlCatePo> findAllTreeCache() {
		return findAllTree();
	}

	@SKGCacheable(db = RedisConstants.SYSTEM, key = "findAllCache")
	public List<GlCatePo> findAllCache() {
		List<GlCatePo> glCatePos = glCateQueryDao.findAll();
		return glCatePos;
	}

	@SKGCacheable(db = RedisConstants.SYSTEM, key = "${id}", params = "id")
	public GlCatePo findGlCatePoById(String id) {
		List<GlCatePo> glCatePos = findAllCache();
		if (glCatePos != null) {
			for (GlCatePo po : glCatePos) {
				if (po.getId().equals(id)) {
					return po;
				}
			}
		}
		return null;
	}

	public GlCate getByCateKey(String typeKey, String cateKey) {
		GlCatePo glCatePo = glCateQueryDao.getByCateKey(typeKey, cateKey);
		if (glCatePo != null) {
			GlCate glCate = SpringHelper.getBean(GlCate.class);
			glCate.setData(glCatePo);
			return glCate;
		}
		return null;
	}

	public List<GlCatePo> findHomeStyles() {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndDepth(GlCateTypeKeys.HomeStyle.key(), 1);
		return glCatePos;
	}

	public List<GlCatePo> findHomeStyles(String parentKey, Integer stat, String bizType, String seoType) {
		GlCatePo parent = glCateQueryDao.getByCateKey(GlCateTypeKeys.HomeStyle.key(), parentKey);
		if (parent != null) {
			List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndParentId(GlCateTypeKeys.HomeStyle.key(), parent.getId(), stat, bizType, seoType);
			return glCatePos;
		} else {
			return null;
		}
	}

	public List<GlCatePo> findHomeStylesAllTree() {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKey(GlCateTypeKeys.HomeStyle.key());
		return convertToTree(glCatePos);
	}

	@Override
	public List<GlCatePo> findHomeCatesAll() {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKey(GlCateTypeKeys.HomeCate.key());
		return glCatePos;
	}

	public List<GlCatePo> findHomeCates() {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndDepth(GlCateTypeKeys.HomeCate.key(), 1);
		return glCatePos;
	}

	public List<GlCatePo> findHomeCates(String parentKey, Integer stat, String bizType, String seoType) {
		GlCatePo parent = glCateQueryDao.getByCateKey(GlCateTypeKeys.HomeCate.key(), parentKey);
		if (parent != null) {
			List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndParentId(GlCateTypeKeys.HomeCate.key(), parent.getId(), stat, bizType, seoType);
			return glCatePos;
		} else {
			return null;
		}
	}

	public List<GlCatePo> findWholeStyles() {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndDepth(GlCateTypeKeys.HomeStyle.key(), 1);
		return glCatePos;
	}

	public List<GlCatePo> findWholeCates(String parentKey, Integer stat, String bizType, String seoType) {
		GlCatePo parent = glCateQueryDao.getByCateKey(GlCateTypeKeys.WholeCate.key(), parentKey);
		if (parent != null) {
			List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndParentId(GlCateTypeKeys.WholeCate.key(), parent.getId(), stat, bizType, seoType);
			return glCatePos;
		} else {
			return null;
		}
	}
	public List<GlCatePo> findWholeCates() {
		List<GlCatePo> glCatePos = glCateQueryDao.findByTypeKeyAndDepth(GlCateTypeKeys.WholeCate.key(), 1);
		return glCatePos;
	}

	@Override
	public GlCatePo findCatePoByKey(String cateKey, Integer stat, String bizType, String seoType) {
		List<GlCatePo> glCatePos = glCateQueryDao.findByCateKey(cateKey, stat, bizType, seoType);
		if (glCatePos != null && glCatePos.size() > 0)
			return glCatePos.get(0);
		else
			return null;
	}

	public List<GlCatePo> findByTypeKeyAndParentId(String typeKey, String parentId, Integer stat, String bizType, String seoType) {
		return glCateQueryDao.findByTypeKeyAndParentId(typeKey, parentId, stat, bizType, seoType);
	}

	@Override
	public GlCatePo getCatePosByKey(String key, Integer star, String bizType, String seoType) {
		GlCatePo glCatePo = findCatePoByKey(key, star, bizType, seoType);
		if (glCatePo != null) {
			getSubs(glCatePo, star, bizType, seoType);
		}
		return glCatePo;
	}

	private void getSubs(GlCatePo glCatePo, Integer star, String bizType, String seoType) {
		List<GlCatePo> subs = findByTypeKeyAndParentId(glCatePo.getTypeKey(), glCatePo.getId(), star, bizType, seoType);
		if (subs != null && !"AppliancesCate".equals(glCatePo.getTypeKey()) ) {
			for (GlCatePo po : subs) {
				getSubs(po, star, bizType, seoType);
			}			
		}
		glCatePo.setSubs(subs);
	}

	@Override
	public GlCatePo getCatePoByChild(String key) {

		return glCateQueryDao.getCatePoByChild(key);
	}

	@Override
	public List<GlCatePo> findAllPaged(Map<String, Object> buildMap, Page page) {
		// TODO Auto-generated method stub
		return glCateQueryDao.findAllPaged(buildMap, page);
	}

	@Override
	public List<GlCatePo> findByParentId(String parentId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId", parentId);
		return glCateQueryDao.findByKey("findByParentId", params);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * com.skg.biz.gl.repository.GlCateRepository#getCateKeyCombox(java.lang
	 * .String)
	 */
	@Override
	public List<GlCatePo> getCateKeyCombox(String dataType) {
		// TODO Auto-generated method stub
		return glCateQueryDao.findByTypeKey(dataType);
	}

	@Override
	public List<GlCatePo> findIdByParentName(String qtTypeId) {
		return glCateQueryDao.findIdByParentName(qtTypeId);
	}

	@Override
	public List<GlCatePo> findAllParentByKey(String key) {
		Map<String, Object> params = new HashMap<>();
		params.put("key", key);
		List<GlCatePo> glCatePos = glCateQueryDao.findByKey("getByKeySigle", params);
		if (glCatePos != null && glCatePos.size() > 0) {
			List<String> ids = StringCollections.toList(glCatePos.get(0).getPath(), ".");
			ids.remove(0);
			List<GlCatePo> parentGlCatePos = glCateQueryDao.findByIdsOrder(ids);
			return parentGlCatePos;
		} else {
			return null;
		}
	}

	@Override
	public List<GlCatePo> findCateSyncList(Page page) {
		List<GlCatePo> pos = glCateQueryDao.findCateSyncList(page);
		List<GlCatePo> newPos = new ArrayList<GlCatePo>();
		for (GlCatePo po : pos) {

			GlCatePo newPo = new GlCatePo();
			newPo = po;
			int count = glCateQueryDao.findCateSyncByParentId(po.getId());
			if (count > 0) {
				newPo.setIsLeaf("N");
			} else {
				newPo.setIsLeaf("Y");
			}
			if (po.getPath().contains(".")) {
				String[] ids = po.getPath().split("[.]");
				List list = glCateQueryDao.findCateSyncByPath(ids);
				String fString=list.toString();
				newPo.setFullParentName(fString.substring(1,fString.length()-1).replaceAll(",","."));
			} else {
				newPo.setFullParentName(po.getName());
			}

			newPos.add(newPo);
		}
		return newPos;
	}

	@Override
	public int findCateSyncListCount() {
		return glCateQueryDao.findCateSyncListCount();
	}

	@Override
	public List<GlCatePo> findByTypeKey(String typeKey) {
		return glCateQueryDao.findByTypeKey(typeKey);
	}

	@Override
	public List<GlCatePo> findGlCateByDepthAndTypeKey(String depth, String typeKey) {
		// TODO Auto-generated method stub
		return glCateQueryDao.findGlCateByDepthAndTypeKey(depth, typeKey);
	}

	@Override
	public List<GlCatePo> parentFindChildKeyAndSelf(List<String> cateKeysList) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("keys", cateKeysList);
		return glCateQueryDao.findByKey("parentKeyFindChildKeyAndSelf", params);
	}

	@Override
	public GlCatePo findCateByTypeKeyAndKey(String typeKey, String key) {
		return glCateQueryDao.getByCateKey(typeKey, key);
	}

	@Override
	public List<GlCatePo> findCateByPath(String path,String tag) {
		return glCateQueryDao.findCateByPath(path,tag);
	}

	@Override
	public List<GlCatePo> findByAirs(Map<String, Object> params) {
		return glCateQueryDao.findByKey("findByAirs", params);
	}

	@Override
	public List<GlCatePo> getRouteCityContent(String cateIds) {
		Map<String, Object> params = new HashMap<>();
		params.put("cateIds", cateIds);
		return glCateQueryDao.findByKey("getRouteCityContent",params);
	}

	@Override
	public List<GlCatePo> findByShopAndCateType(String shopId, String cateType) {
		Map<String, Object> params = new HashMap<>();
		params.put("shopId", shopId);
		params.put("cateType", cateType);
		return glCateQueryDao.findByKey("findByShopAndCateType",params);
	}
}
